import 'package:flutter/material.dart';
import 'package:hkzf/pages/home/info/index.dart';
import 'package:hkzf/pages/room_detail/data.dart';
import 'package:hkzf/widgets/common_swiper.dart';
import 'package:hkzf/widgets/common_tag.dart';
import 'package:hkzf/widgets/common_title.dart';
import 'package:hkzf/widgets/room_appliance.dart';
import 'package:share/share.dart';
import 'dart:math';

TextStyle buttonStyle = TextStyle(fontSize: 20, color: Colors.white);

class RoomDetailPage extends StatefulWidget {
  final roomId;
  RoomDetailPage({Key key, this.roomId}) : super(key: key);

  @override
  _RoomDetailPageState createState() => _RoomDetailPageState();
}

class _RoomDetailPageState extends State<RoomDetailPage> {
  RoomDetailData data;
  bool isLike = false;
  bool showAllText = false;

  @override
  void initState() {
    setState(() {
      data = defaultData;
      isLike = true;
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if (data == null) {
      return Container();
    }
    bool showTextTool = data.subTitle.length > 100;
    return Scaffold(
      appBar: AppBar(
        title: Text('roomid:${widget.roomId}'),
        actions: [
          IconButton(
              icon: Icon(Icons.share),
              onPressed: () {
                Share.share('https://www.baidu.com');
              })
        ],
      ),
      body: SafeArea(
        child: Stack(
          children: [
            ListView(children: [
              Container(
                height: 200,
                child: CommonSwiper(
                  images: data.houseImgs,
                ),
              ),
              CommonTitle(data.title),
              Container(
                padding: EdgeInsets.only(left: 10),
                child:
                    Row(crossAxisAlignment: CrossAxisAlignment.end, children: [
                  Text(
                    data.price.toString(),
                    style: TextStyle(fontSize: 20, color: Colors.pink),
                  ),
                  Text(
                    '元/月（押一付三）',
                    style: TextStyle(fontSize: 14, color: Colors.pink),
                  )
                ]),
              ),
              Container(
                padding: EdgeInsets.only(left: 10, top: 6, bottom: 6),
                child: Wrap(
                    spacing: 4,
                    children:
                        data.tags.map((item) => CommonTag(item)).toList()),
              ),
              Divider(
                indent: 10,
                endIndent: 10,
                color: Colors.grey,
              ),
              Container(
                padding: EdgeInsets.only(left: 10, top: 6, bottom: 6),
                child: Wrap(spacing: 10, runSpacing: 10, children: [
                  BaseInfoItem(data: '面积:${data.size}平方米'),
                  BaseInfoItem(data: '楼层:${data.floor}'),
                  BaseInfoItem(data: '房型:${data.roomType}'),
                  BaseInfoItem(data: '装修:${data.size}'),
                ]),
              ),
              CommonTitle('房屋配置'),
              RoomApplicanceList(list: data.applicances),
              CommonTitle('房屋概况'),
              Container(
                padding: EdgeInsets.only(left: 10, right: 10),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(data.subTitle ?? '暂无房屋概况',
                        maxLines: showAllText ? null : 5),
                    Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          GestureDetector(
                            onTap: () {
                              setState(() {
                                showAllText = !showAllText;
                              });
                            },
                            child: Row(
                              children: [
                                showTextTool
                                    ? Text(showAllText ? '收起' : '展开')
                                    : null,
                                Icon(showAllText
                                    ? Icons.keyboard_arrow_up
                                    : Icons.keyboard_arrow_down)
                              ],
                            ),
                          ),
                          Text('举报')
                        ])
                  ],
                ),
              ),
              CommonTitle('猜你喜欢'),
              Info(),
              Padding(
                padding: EdgeInsets.only(bottom: 100),
              )
            ]),
            Positioned(
              bottom: 0,
              child: Container(
                width: MediaQuery.of(context).size.width,
                height: 100,
                color: Colors.grey,
                padding: EdgeInsets.all(10),
                child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      GestureDetector(
                        onTap: () {
                          setState(() {
                            isLike = !isLike;
                          });
                        },
                        child: Container(
                          height: 50,
                          width: 40,
                          margin: EdgeInsets.only(right: 10),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Icon(
                                isLike ? Icons.star : Icons.star_border,
                                size: 24,
                                color: isLike ? Colors.green : Colors.black,
                              ),
                              Text(
                                isLike ? '已收藏' : '收藏',
                                style: TextStyle(fontSize: 12),
                              )
                            ],
                          ),
                        ),
                      ),
                      Expanded(
                        child: GestureDetector(
                          child: Container(
                            height: 50,
                            decoration: BoxDecoration(
                                color: Colors.green,
                                borderRadius: BorderRadius.circular(6)),
                            child: Center(
                              child: Text(
                                '联系房东',
                                style: buttonStyle,
                              ),
                            ),
                          ),
                        ),
                      ),
                      Padding(
                        padding: EdgeInsets.only(left: 10),
                      ),
                      Expanded(
                        child: GestureDetector(
                          child: Container(
                            height: 50,
                            decoration: BoxDecoration(
                                color: Colors.cyan,
                                borderRadius: BorderRadius.circular(6)),
                            child: Center(
                              child: Text(
                                '预约看房',
                                style: buttonStyle,
                              ),
                            ),
                          ),
                        ),
                      ),
                    ]),
              ),
            )
          ],
        ),
      ),
    );
  }
}

class BaseInfoItem extends StatelessWidget {
  const BaseInfoItem({
    Key key,
    @required this.data,
  }) : super(key: key);

  final String data;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text(
        data,
        style: TextStyle(fontSize: 16),
      ),
      width: (MediaQuery.of(context).size.width - 30) / 2,
    );
  }
}
